package com.ekuaitu.kuaitustaff.baiduMap;

import android.content.Context;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiIndoorResult;
import com.baidu.mapapi.search.poi.PoiNearbySearchOption;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.baidu.mapapi.search.poi.PoiSortType;
import com.ekuaitu.kuaitustaff.application.MyApplication;
import com.ekuaitu.kuaitustaff.utils.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class MapPoiSearch implements OnGetPoiSearchResultListener {
    private BaiduMap mBaiduMap;
    private Context mContext;
    public PoiResult poiResult;
    private boolean isNavi = false;
    private MyPoiOverlay overlay;
    private static MapPoiSearch instance;
    private PoiSearch mPoiSearch;
    private int bitmapResource;
    private boolean isshowChargingPoint;//是否展示充电桩
    public List<Map<String, Double>> latLngList = new ArrayList<Map<String, Double>>();
    public List<Map<String, Double>> alllatLngList = new ArrayList<>();
    private String keyword;
    private LatLng carPosition;
    private int radius;

    public static MapPoiSearch getInstance() {
        if (instance == null) {
            instance = new MapPoiSearch();
        }
        return instance;
    }

    private onGetResult onGetResult;

    public MapPoiSearch() {

    }


    public void searchMarker(BaiduMap mBaiduMap, Context mContext, LatLng carPosition, String keyword, int radius, int bitmapResource, boolean isshowChargingPoint, onGetResult onGetResult) {
        mPoiSearch = PoiSearch.newInstance();
        this.mBaiduMap = mBaiduMap;
        this.mContext = mContext;
        this.bitmapResource = bitmapResource;
        this.isshowChargingPoint = isshowChargingPoint;
        this.keyword = keyword;
        this.carPosition = carPosition;
        this.radius = radius;
        this.onGetResult = onGetResult;
        mPoiSearch.setOnGetPoiSearchResultListener(instance);
        mPoiSearch.searchNearby(new PoiNearbySearchOption()
                .keyword(keyword)
                .sortType(PoiSortType.distance_from_near_to_far)
                .location(carPosition)
                .radius(radius)
                .pageCapacity(50).pageNum(0)
        );
    }


    @Override
    public void onGetPoiResult(PoiResult result) {
        Logger.i("search", result.toString() + result.error + "");
        if (result.error == SearchResult.ERRORNO.SEARCH_SERVER_INTERNAL_ERROR || result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {
            mPoiSearch.searchNearby(new PoiNearbySearchOption()
                    .keyword(keyword)
                    .sortType(PoiSortType.distance_from_near_to_far)
                    .location(carPosition)
                    .radius(radius)
                    .pageCapacity(50).pageNum(0));
        }
        if (result.error == SearchResult.ERRORNO.NO_ERROR) {
            //mBaiduMap.clear();
            if ("236".equals(MyApplication.getInstance().getAreaCode())) {
                if ("特来电充电站".equals(keyword) || "交运充电桩".equals(keyword)) {
                    PoiInfo poiInfo = new PoiInfo();
                    poiInfo.address = "青岛市南区南京路与宁夏路交汇路口120号裕丰大厦 ";
                    poiInfo.name = "丽枫酒店充电站";
                    poiInfo.location = new LatLng(36.089348, 120.392139);
                    result.getAllPoi().add(poiInfo);

                    PoiInfo poiInfoSecond = new PoiInfo();
                    poiInfoSecond.address = "青岛市崂山区崂山区同兴路710号 ";
                    poiInfoSecond.name = "和润世佳商务酒店";
                    poiInfoSecond.location = new LatLng(36.111197, 120.437072);
                    result.getAllPoi().add(poiInfoSecond);
                }
            }
            if (isshowChargingPoint) {
                overlay = new MyPoiOverlay(mBaiduMap, mPoiSearch, bitmapResource);
                mBaiduMap.setOnMarkerClickListener(overlay);
                overlay.setData(result);
                overlay.addToMap();
                overlay.zoomToSpan();
         /*  int totalPage = result.getTotalPageNum();// 获取总分页数
              Toast.makeText(mContext,"总共查到" + result.getTotalPoiNum() + "个兴趣点, 分为" + totalPage + "页", Toast.LENGTH_SHORT).show();
      */
                poiResult = result;
                isNavi = true;
                if (result.getAllPoi().size() != 0) {
                    for (int i = 0; i < result.getAllPoi().size(); i++) {
                        HashMap<String, Double> map = new HashMap<>();
                        map.put("latitude", result.getAllPoi().get(i).location.latitude);
                        map.put("longitude", result.getAllPoi().get(i).location.longitude);
                        latLngList.add(map);
                    }
                    alllatLngList.addAll(latLngList);
                }

                onGetResult.getResult();
            } else {
                //获取充电桩 经纬度  开门检查传给后台 返回电桩周围工单
                if (result.getAllPoi().size() != 0) {
                    for (int i = 0; i < result.getAllPoi().size(); i++) {
                        HashMap<String, Double> map = new HashMap<>();
                        map.put("latitude", result.getAllPoi().get(i).location.latitude);
                        map.put("longitude", result.getAllPoi().get(i).location.longitude);
                        latLngList.add(map);
                    }
                    alllatLngList.addAll(latLngList);
                    //   ToastUtils.makeText(mContext,alllatLngList.size(),ToastUtils.LENGTH_SHORT).show();
                }
            }
        }
        if (result.error == SearchResult.ERRORNO.AMBIGUOUS_KEYWORD) {
            // 当输入关键字在本市没有找到，但在其他城市找到时，返回包含该关键字信息的城市列表
            String strInfo = "在";
            strInfo += "找到结果";
        }
    }

    @Override
    public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {

    }

    @Override
    public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {

    }

    public interface onGetResult {
        void getResult();
    }
}



